import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { faker } from "@faker-js/faker/locale/zh_CN";
import { getData as getRoles } from "./role";
import { getData as getDepts } from "./dept";

let hasInit = false;

const getData = () => {
  const data: any[] = [
    {
      id: 1,
      username: "admin",
      nickname: "系统管理员",
      depts: [
        {
          id: 11,
          deptName: "上海分公司研发部门"
        },
        {
          id: 12,
          deptName: "上海分公司市场部门"
        }
      ],
      roles: [getRoles()[0]],
      status: true
    },
    {
      id: 2,
      username: "common",
      nickname: "普通用户",
      depts: [],
      roles: [getRoles()[1]],
      status: false
    }
  ];
  if (!hasInit) {
    hasInit = true;
    for (const index in data) {
      data[index] = {
        ...data[index],
        phone: faker.phone.number(),
        email: faker.internet.email(),
        remark: faker.book.genre(),
        sort: "99",
        createTime: faker.date.between({ from: "2000-01-01", to: Date.now() }),
        updateTime: faker.date.between({ from: "2000-01-01", to: Date.now() })
      };
    }
  }
  return data.sort((a, b) => a.sort - b.sort);
};

export default defineFakeRoute([
  {
    url: "/system/users/roles",
    method: "get",
    //statusCode: 404,
    response: () => {
      return {
        success: true,
        message: "操作成功",
        data: getRoles()
      };
    }
  },
  {
    url: "/system/users/depts",
    method: "get",
    //statusCode: 404,
    response: () => {
      return {
        success: true,
        message: "操作成功",
        data: getDepts()
      };
    }
  },
  {
    url: "/system/users/page",
    method: "post",
    //statusCode: 404,
    response: ({ body }) => {
      const data = getData();
      return {
        success: true,
        message: "操作成功",
        data: {
          list: data.slice(
            (body.page - 1) * body.pageSize,
            body.page * body.pageSize
          ),
          total: data.length,
          pageSize: body.pageSize,
          currentPage: body.page
        }
      };
    }
  },
  {
    url: "/system/users",
    method: "post",
    response: ({ body }) => {
      return {
        success: true,
        message: "操作成功",
        data: {
          ...body,
          id: 999
        }
      };
    }
  },
  {
    url: "/system/users/:id",
    method: "get",
    response: ({ params }) => {
      return {
        success: true,
        message: "操作成功",
        data: getData().find(item => item.id == params.id)
      };
    }
  },
  {
    url: "/system/users/:id",
    method: "put",
    response: ({ params }) => {
      return {
        success: true,
        message: "操作成功",
        data: getData().find(item => item.id == params.id)
      };
    }
  },
  {
    url: "/system/users/:id/password",
    method: "put",
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/system/users/:id/roles",
    method: "get",
    response: ({ params }) => {
      return {
        success: true,
        message: "操作成功",
        data: getRoles().filter(role => role.id == params.id)
      };
    }
  },
  {
    url: "/system/users/:id/roles",
    method: "put",
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/system/users/:id/status",
    method: "put",
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/system/users/:id",
    method: "delete",
    statusCode: 404,
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/system/users",
    method: "delete",
    response: () => {
      return {
        success: false,
        message: "禁止删除"
      };
    }
  }
]);
